//
//  FreebaseCollection.h
//  Freebase
//
//  Created by Alec Flett on 10/15/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Freebase.h"
#import "FreebaseDelegate.h"


@protocol FreebaseCollectionDelegate

- (void)pageLoaded:(NSInteger)page data:(NSArray*)data;
- (void)itemTotalLoaded:(NSInteger)count;

@end


// a view-independent, lazily loading collection from a query
@interface FreebaseCollection : NSObject <FreebaseDelegate> {
    FreebaseSession *fbs;
    NSMutableArray *_pageData; // array of arrays
    NSMutableArray *_pageStartIndexes; // the index of the start of each page
    NSMutableArray *_cursors; // cursor strings for each pageSize, so that cursor[n] is the cursor for entries starting at n*pageSize
    NSInteger _item_count;
    NSUInteger _pageSize;
    NSMutableSet *_pendingCursors; // cursors that are loading right this moment
        
    NSArray *_query;
    
    id<FreebaseCollectionDelegate> _delegate;
    
}

- (FreebaseCollection*)initWithDelegate:(id<FreebaseCollectionDelegate>)delegate;

- (id)itemAtIndex:(NSUInteger)index;
- (NSInteger)rowsInSection:(NSInteger)section;
- (id<FreebaseRequest>)loadItemsFromQuery: (id)q;
- (NSInteger)pageStart: (NSInteger)page;

@property(retain, nonatomic) FreebaseSession* fbs;
@property(retain, nonatomic) NSMutableArray *pageData;
@property(retain, nonatomic) NSMutableArray *cursors;
@property(retain, nonatomic) NSArray *query;
@property(retain, nonatomic) NSMutableSet *pendingCursors;
@property(nonatomic) NSUInteger pageSize;

@end
